//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - S+S Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.CompositeUI.WPF;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeUI.Commands;

using LitwareHR.Infrastructure.Interface;
using System.Windows.Media.Imaging;
using Microsoft.Practices.CompositeUI.Utility;

namespace LitwareHR.PublicApp.WinUX
{
    /// <summary>
    /// Interaction logic for linkView.xaml
    /// </summary>
    public partial class LinkView : System.Windows.Controls.UserControl, ILinkView, IDisposable
    {
        /// <summary>
        /// Initialize a new instance of <see cref="linkView"/>.
        /// <summary>
        public LinkView()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(OnLoad);
        }

        /// <summary>
        /// Called when any link button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Button_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            _presenter.RaiseShowView(this, new EventArgs<string>(button.CommandParameter.ToString()));

            // Code to Reset Link Button Style to default
            foreach (DockPanel dockPanel in ((StackPanel)(((DockPanel)(button.Parent)).Parent)).Children)
            {
                for (int count = 0; count < dockPanel.Children.Count; count++)
                {
                    if (dockPanel.Children[count].GetType() == typeof(Button))
                    {
                        Button btn = (Button)dockPanel.Children[count];
                        // Set Style = "NormalLinkButtonStyle"
                        btn.Style = (Style)TryFindResource("NormalLinkButtonStyle");
                    }
                }
            }
            // Code to Highlight selected Link Button using "PressedLinkButtonStyle" Style
            button.Style = (Style)TryFindResource("PressedLinkButtonStyle");
        }

        /// <summary>
        /// Called when WPF has loaded the control and is ready to display it.
        /// <summary>
        /// <param name="sender">Source of the event</param>
        /// <param name="e">Extra information about the event</param>
        void OnLoad(object sender, RoutedEventArgs e)
        {
            _presenter.OnViewReady();
            // Merge resoure directory
            base.Resources.MergedDictionaries.Add((ResourceDictionary)Application.LoadComponent(new Uri(@"Litware.HR;;;component/Views/Skins/BaseSkin.xaml", UriKind.Relative)));
        }

        /// <summary>
        /// This method creates new Button similar to the SampleButton (defined in xaml)
        /// </summary>
        /// <param name="command"></param>
        /// <param name="iconName"></param>
        /// <returns></returns>
        private Button CreateButton(string command, string iconName)
        {
            // Create Bitmap Image depending upon the iconName.
            // This image will be displayed on the button
            Image image = new Image();
            string strIconSource = "../../Images/" + iconName;
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            bitmapImage.UriSource = new Uri(strIconSource, UriKind.Relative);
            bitmapImage.EndInit();
            image.Stretch = Stretch.Fill;
            image.Source = bitmapImage;
            image.Width = SampleButtonIcon.Width;
            image.Height = SampleButtonIcon.Height;
            
            Button button = new Button();
            button.Height = SampleButton.Height;
            button.Width = SampleButton.Width;
            button.BorderThickness = SampleButton.BorderThickness;
            button.Margin = SampleButton.Margin;
            button.Padding = SampleButton.Padding;
            button.FontSize = SampleButton.FontSize;
            button.FontWeight = SampleButton.FontWeight;
            button.Background = SampleButton.Background;
            button.Foreground = SampleButton.Foreground;
            button.Style = SampleButton.Style;
            button.HorizontalContentAlignment = SampleButton.HorizontalContentAlignment;
            // Add Image as content of this button
            button.Content = image;
            button.CommandParameter = command;
            return button;
        }

        /// <summary>
        /// This method is used to create Text to be displayed next to the command buttons.
        /// </summary>
        /// <param name="content"></param>
        /// <param name="command"></param>
        /// <returns></returns>
        private TextBlock CreateTextBlock(string content, string command)
        {
            TextBlock textBlock = new TextBlock();
            textBlock.Height = SampleButtonName.Height;
            textBlock.Foreground = SampleButtonName.Foreground;
            textBlock.VerticalAlignment = SampleButtonName.VerticalAlignment;
            textBlock.Margin = SampleButtonName.Margin;
            textBlock.Text = content;
            
            return textBlock;
        }

        #region ILinkView Members

        /// <summary>
        /// This method creates all Link Buttons and adds it to LinkView
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void BindData(CommandMap commandMap)
        {
            Guard.ArgumentNotNull(commandMap, "commandMap");

            foreach (Mapping map in commandMap.Mapping)
            {
                string rolesString = Properties.Settings.Default.Roles;
                bool showButton = false;

                foreach (string role in map.Roles)
                {
                    showButton = showButton || ((rolesString.Contains(role)) || (role == "*"));
                }
                if (showButton)
                {
                    Button button = CreateButton(map.CommandName, map.Icon);
                    button.Click += new RoutedEventHandler(Button_Click);

                    TextBlock textBlock = CreateTextBlock(map.Label, map.CommandName);
                    DockPanel dockPanel = new DockPanel();
                    DockPanel.SetDock(button, Dock.Left);
                    DockPanel.SetDock(textBlock, Dock.Left);

                    // Add button and textblock to DockPanel
                    dockPanel.Children.Add(button);
                    dockPanel.Children.Add(textBlock);

                    // Add Dock Panel to the Stack Panel
                    panel.Children.Add(dockPanel);
                }
            }
            panel.Children.Remove(SampleDockPanel);
        }

        #endregion
    }
}

